/**
 * 接听页
 * page接听页
 */
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/live/answerPhone.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/anchorInfoModel.dart';
import 'package:qgchat/http/models/listuserInfoModel.dart';
import 'package:qgchat/states/states_model/index.dart';

class AnswerPhone extends StatefulWidget {
  AnswerPhone({Key key}) : super(key: key);

  @override
  _AnswerPhoneState createState() => _AnswerPhoneState();
}

class _AnswerPhoneState extends State<AnswerPhone> {
  AnswerPhoneBloc bloc;

  @override
  Widget build(BuildContext context) {
    final Map args = ModalRoute.of(context).settings.arguments;
    int targetInv = args["targetInv"];
    int targetId = args["targetId"];
    String targetName = args["targetName"];
    String targetHead = args["targetHead"];
    String targetToken = args["targetToken"];
    if (bloc == null) {
      bloc = AnswerPhoneBloc(targetToken, targetInv);
    }
    return BlocProvider(
      bloc: bloc,
      child: AnswerPhoneBuild(
        targetInv,
        targetId,
        targetName,
        targetHead,
        targetToken,
      ),
    );
  }
}

class AnswerPhoneBuild extends StatelessWidget {
  AnswerPhoneBuild(
    this.targetInv,
    this.targetId,
    this.targetName,
    this.targetHead,
    this.targetToken, {
    Key key,
  }) : super(key: key);

  final int targetInv;
  final int targetId;
  final String targetName;
  final String targetHead;
  final String targetToken;

  AnswerPhoneBloc bloc;

  @override
  Widget build(BuildContext context) {
    bloc = BlocProvider.of<AnswerPhoneBloc>(context);

    return Scaffold(
      body: Stack(
        children: <Widget>[
          bg(context),
          frostedGlass,
          content(context),
        ],
      ),
    );
  }

  // 底片
  Widget bg(context) {
    return Container(
      width: w100(context),
      height: h100(context),
      child: CachedNetworkImage(
        imageUrl: "$targetHead",
        fit: BoxFit.cover,
        errorWidget: (context, url, error) {
          return Config.errorNetImage;
        },
      ),
    );
  }

  // 毛玻璃层
  final Widget frostedGlass = BackdropFilter(
    filter: ImageFilter.blur(sigmaX: 30, sigmaY: 30),
    child: Container(
      color: Colors.white.withAlpha(0),
    ),
  );

  // 用户数据构造函数
  Widget userDataBuild({title, count, unit}) {
    return Container(
      height: px(100),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            "$title",
            style: TextStyle(
              height: .99,
              color: Colors.white,
              fontSize: px(28),
            ),
          ),
          RichText(
            text: TextSpan(
              text: "$count",
              style: TextStyle(
                height: .99,
                color: Colors.white,
                fontSize: px(50),
              ),
              children: <TextSpan>[
                TextSpan(
                  text: "$unit",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(32),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  // 页面内容
  Widget content(context) {
    return Container(
      width: w100(context),
      height: h100(context),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: px(118)),
            child: Column(
              children: <Widget>[
                Container(
                  width: px(180),
                  height: px(180),
                  child: ClipOval(
                    child: CachedNetworkImage(
                      imageUrl: "$targetHead",
                      fit: BoxFit.cover,
                      errorWidget: (context, url, error) {
                        return Config.errorNetImage;
                      },
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: px(39)),
                  child: Text(
                    "$targetName",
                    style: TextStyle(
                      height: .99,
                      color: Colors.white,
                      fontSize: px(38),
                    ),
                  ),
                ),
                StreamBuilder(
                  stream: bloc.userTypeStream,
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (!snapshot.hasData) {
                      return Container();
                    }
                    // 主播样式
                    if (snapshot.data) {
                      return Container(
                        margin: EdgeInsets.only(top: px(55)),
                        child: StreamBuilder(
                          stream: bloc.costStream,
                          initialData: bloc.costDefault,
                          builder: (
                            BuildContext context,
                            AsyncSnapshot snapshot,
                          ) {
                            AnchorInfoModel cost = snapshot.data;
                            return Text(
                              "该主播时光价值 ${cost.timePrice} 香币/分钟",
                              style: TextStyle(color: Colors.white),
                            );
                          },
                        ),
                      );
                    } else {
                      // 用户样式
                      return Container(
                        margin: EdgeInsets.only(top: px(55)),
                        child: StreamBuilder(
                          stream: bloc.consumeStream,
                          initialData: bloc.consumeDefault,
                          builder: (
                            BuildContext context,
                            AsyncSnapshot snapshot,
                          ) {
                            ListuserInfoModel count = snapshot.data;
                            return Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                userDataBuild(
                                  title: "为你消费",
                                  count: count.gold1,
                                  unit: "金币",
                                ),
                                userDataBuild(
                                  title: "财富余额",
                                  count: count.money,
                                  unit: "金币",
                                ),
                              ],
                            );
                          },
                        ),
                      );
                    }
                  },
                ),
              ],
            ),
          ),
          Container(
            width: px(690),
            margin: EdgeInsets.only(bottom: px(180)),
            child: Column(
              children: <Widget>[
                Text(
                  "向你发起视频通话请求",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(26),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: px(90)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Container(
                        height: px(200),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              width: px(150),
                              height: px(150),
                              child: FlatButton(
                                onPressed: () {
                                  bloc.refuse(
                                    targetToken: targetToken,
                                    inv: targetInv,
                                  );
                                },
                                padding: EdgeInsets.all(0),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(
                                    sp(75),
                                  ),
                                ),
                                child: Image.asset(
                                  "assets/images/icon/oncall.png",
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Text(
                              "拒绝",
                              style: TextStyle(
                                height: .99,
                                color: Colors.white,
                                fontSize: px(32),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        height: px(200),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              width: px(150),
                              height: px(150),
                              child: FlatButton(
                                onPressed: () {
                                  UserState userState = state(context, 'user');
                                  int aid;
                                  int uid;
                                  bool isAnchor =
                                      userState.info.memberType == 1;
                                  if (isAnchor) {
                                    // 本人是主播
                                    aid = userState.info.id;
                                    uid = targetId;
                                  } else {
                                    // 本人是用户
                                    aid = targetId;
                                    uid = userState.info.id;
                                  }
                                  bloc.accept(
                                    context,
                                    isAnchor,
                                    targetInv,
                                    uid,
                                    aid,
                                    targetName,
                                    targetHead,
                                    targetToken,
                                  );
                                },
                                padding: EdgeInsets.all(0),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(
                                    sp(75),
                                  ),
                                ),
                                child: Image.asset(
                                  "assets/images/icon/notcall.png",
                                ),
                              ),
                            ),
                            Text(
                              "接听",
                              style: TextStyle(
                                height: .99,
                                color: Colors.white,
                                fontSize: px(32),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
